package mirkofrancuski.battleship;

import mirkofrancuski.battleship.db.DatabaseHelper;
import mirkofrancuski.battleship.game.Board;
import mirkofrancuski.battleship.gui.PlacementBoard;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class PlacementActivity extends Activity {
	private String DEBUG_TAG = "DEBUG PlacementActivity";
	private DatabaseHelper m_dbHelper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		m_dbHelper = new DatabaseHelper(this);

		setContentView(R.layout.activity_placement);

		// set font
		Button buttonRandomize = (Button) findViewById(R.id.button_random_placement);
		Typeface font = Typeface.createFromAsset(getAssets(), "ArmyChalk.ttf");
		buttonRandomize.setTypeface(font);

		Button buttonStart = (Button) findViewById(R.id.button_start_battle);
		font = Typeface.createFromAsset(getAssets(), "ArmyChalk.ttf");
		buttonStart.setTypeface(font);

		WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		int width = display.getWidth();
		int height = display.getHeight();

		if (height - width < 200) {
			
			LayoutParams paramsRandomize = (LayoutParams) buttonRandomize.getLayoutParams();
			paramsRandomize.width = LayoutParams.WRAP_CONTENT;
			paramsRandomize.addRule(RelativeLayout.CENTER_HORIZONTAL,0);
			paramsRandomize.addRule(RelativeLayout.BELOW,R.id.game_board_placement);
			paramsRandomize.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
			paramsRandomize.setMargins(5, 20, 5, 20);
			
			
			LayoutParams paramsStart = (LayoutParams) buttonStart.getLayoutParams();
			paramsStart.width = LayoutParams.WRAP_CONTENT;
			paramsStart.addRule(RelativeLayout.CENTER_HORIZONTAL,0);
			paramsStart.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0);
			paramsStart.addRule(RelativeLayout.BELOW,R.id.game_board_placement);
			paramsStart.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
			paramsStart.setMargins(5, 20, 5, 20);
		}
	}

	@Override
	protected void onDestroy() {
		Log.d(DEBUG_TAG, "onDestory");
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		Log.d(DEBUG_TAG, "onPause");
		super.onPause();
		PlacementBoard gameBoard = (PlacementBoard) findViewById(R.id.game_board_placement);
		m_dbHelper.savePlacementBoard(gameBoard.getBoard());
	}

	@Override
	protected void onResume() {
		Log.d(DEBUG_TAG, "onResume");
		super.onResume();

		PlacementBoard gameBoard = (PlacementBoard) findViewById(R.id.game_board_placement);
		Board board = m_dbHelper.restorePlacementBoard();
		if (board != null)
			gameBoard.setBoard(board);
	}

	@Override
	protected void onRestart() {
		Log.d(DEBUG_TAG, "onRestart");
		super.onRestart();
	}

	@Override
	protected void onStart() {
		Log.d(DEBUG_TAG, "onStart");
		super.onStart();

	}

	@Override
	protected void onStop() {
		Log.d(DEBUG_TAG, "onStop");
		super.onStop();
	}

	public void randomize(View view) {

		PlacementBoard gameBoard = (PlacementBoard) findViewById(R.id.game_board_placement);
		gameBoard.randomPlacement();

	}

	public void startBattle(View view) {

		PlacementBoard gameBoard = (PlacementBoard) findViewById(R.id.game_board_placement);
		Board board = gameBoard.getBoard();
		Intent returnIntent = new Intent();
		returnIntent.putExtra("resultBoard", board);
		setResult(RESULT_OK, returnIntent);
		finish();
	}

}
